package com.woniuxy.config;

import com.woniuxy.util.RabbitMQConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ：admin
 * @version ：1.0
 * @date ：Created in 2025/5/16 9:09
 * @description ：RabbitMQ 消息队列配置类
 * @modified_by ：
 */
@Slf4j
@Configuration
public class RabbitmqConfig {
    /**
     * <p>主题交换机。</p>
     *
     * @return 主题交换机
     */
    @Bean
    public TopicExchange topicExchange() { return new TopicExchange(RabbitMQConst.TOPIC_EXCHANGE); }

    /**
     * <p>将限时抢购商品信息预热到 redis 缓存中的消息队列。</p>
     *
     * @return 消息队列
     */
    @Bean
    public Queue timeSalePreheatQueue() { return new Queue(RabbitMQConst.TIME_SALE_PREHEAT_QUEUE); }

    /**
     * <p>绑定限时抢购商品预热消息队列与主题交换机。</p>
     *
     * @param timeSalePreheatQueue 限时抢购商品预热的消息队列
     * @param topicExchange 主题交换机
     * @return Binding 对象
     */
    @Bean
    public Binding timeSalePreheatQueue2topicExchange(Queue timeSalePreheatQueue, TopicExchange topicExchange) {
        return BindingBuilder
                .bind(timeSalePreheatQueue)
                .to(topicExchange)
                .with(RabbitMQConst.TIME_SALE_PREHEAT_ROUTING_KEY);
    }

    /**
     * <p>添加限时抢购商品到数据库的消息队列。</p>
     *
     * @return 消息队列
     */
    @Bean
    public Queue addTimeSaleQueue() {
        return new Queue(RabbitMQConst.ADD_TIME_SALE_QUEUE);
    }

    /**
     * <p>绑定添加限时抢购商品到数据库的消息队列与主题交换机。</p>
     * @param addTimeSaleQueue
     * @param topicExchange
     * @return
     */
    @Bean
    public Binding addTimeSaleQueue2topicExchange(Queue addTimeSaleQueue, TopicExchange topicExchange) {
        return BindingBuilder
                .bind(addTimeSaleQueue)
                .to(topicExchange)
                .with(RabbitMQConst.ADD_TIME_SALE_ROUTING_KEY);
    }

    /**
     * <p>抢购页面添加购物车消息队列</p>
     *
     * @return 消息队列
     */
    @Bean
    public Queue saveCartQueue() {
        return new Queue(RabbitMQConst.SAVE_CART_QUEUE);
    }

    /**
     * <p>绑定抢购页面添加购物车消息队列和主题交换机。</p>
     *
     * @param saveCartQueue
     * @param topicExchange
     * @return
     */
    @Bean
    public Binding saveCartQueue2topicExchange(Queue saveCartQueue, TopicExchange topicExchange) {
        return BindingBuilder
                .bind(saveCartQueue)
                .to(topicExchange)
                .with(RabbitMQConst.SAVE_CART_ROUTING_KEY);
    }

    @Bean
    public Queue deleteUserOrderQueue(){
        return new Queue(RabbitMQConst.DELETE_USER_ORDER_QUEUE);
    }
    @Bean
    public DirectExchange deleteUserOrderExchange(){
        return new DirectExchange(RabbitMQConst.DELETE_USER_ORDER_EXCHANGE);
    }
    @Bean
    public Binding deleteUserOrderQueueToExchange(Queue deleteUserOrderQueue,DirectExchange deleteUserOrderExchange){
        return BindingBuilder.bind(deleteUserOrderQueue).to(deleteUserOrderExchange).
                with(RabbitMQConst.DELETE_USER_ORDER);
    }
    /**
     * 加入购物车队列
     */
    @Bean
    public Queue addShoppingCartQueue(){
        return new Queue(RabbitMQConst.ADD_SHOPPING_CART_QUEUE);
    }
    @Bean
    public Queue addShoppingCartByProductIdQueue(){
        return new Queue(RabbitMQConst.ADD_SHOPPING_CART_BY_PRODUCT_ID_QUEUE);
    }
    @Bean
    public DirectExchange addShoppingCartExchange(){
        return new DirectExchange(RabbitMQConst.ADD_SHOPPING_CART_EXCHANGE);
    }
    @Bean
    public Binding addShoppingCartQueueToExchange(Queue addShoppingCartQueue,DirectExchange addShoppingCartExchange){
        return BindingBuilder.bind(addShoppingCartQueue).to(addShoppingCartExchange).
                with(RabbitMQConst.ADD_SHOPPING_CART);
    }
    @Bean
    public Binding addShoppingCartByProductIdQueueToExchange(Queue addShoppingCartByProductIdQueue,DirectExchange addShoppingCartExchange){
        return BindingBuilder.bind(addShoppingCartByProductIdQueue).to(addShoppingCartExchange).
                with(RabbitMQConst.ADD_SHOPPING_CART_BY_PRODUCT_ID);
    }

    /**
     * <p>结算时添加订单信息的消息队列</p>
     * @return 消息队列
     */
    @Bean
    public Queue addOrdersFromCartQueue() {
        return new Queue(RabbitMQConst.ADD_ORDERS_QUEUE);
    }

    /**
     * <p>绑定结算时添加订单信息的消息队列和主题交换机。</p>
     * @param addOrdersFromCartQueue
     * @param topicExchange
     * @return
     */
    @Bean
    public Binding addOrdersFromCartQueue2topicExchange(Queue addOrdersFromCartQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(addOrdersFromCartQueue).to(topicExchange).with(RabbitMQConst.ADD_ORDERS_ROUTING_KEY);
    }

    /**
     * 声明延迟交换机 (x-delayed-message类型)
     */
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(
                RabbitMQConst.DELAY_EXCHANGE,
                "x-delayed-message",  // 类型为延迟消息
                true,
                false,
                args);
    }

    /**
     * 声明延迟队列
     */
    @Bean
    public Queue delayQueue() {
        return new Queue(RabbitMQConst.DELAY_QUEUE, true);
    }

    /**
     * 绑定延迟队列到延迟交换机
     */
    @Bean
    public Binding delayQueue2delayExchange(Queue delayQueue, CustomExchange delayExchange) {
        return BindingBuilder.bind(delayQueue)
                .to(delayExchange)
                .with(RabbitMQConst.DELAY_ROUTING_KEY)
                .noargs();
    }

    @Bean
    public Queue addOrderItemListQueue() {
        return new Queue(RabbitMQConst.ADD_ORDER_ITEM_LIST_QUEUE);
    }

    @Bean
    public Binding addOrderItemListQueue2topicExchange(Queue addOrderItemListQueue, TopicExchange topicExchange) {
        return BindingBuilder
                .bind(addOrderItemListQueue)
                .to(topicExchange)
                .with(RabbitMQConst.ADD_ORDER_ITEM_LIST_ROUTING_KEY);
    }
}
